package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView id_tvshow;
    private Button id_btn1,id_btn2,id_btn3,id_btn4,id_btn5,id_btn6,id_btn7,id_btn8,id_btn9,id_btn0;
    private Button id_btnjia,id_btncheng,id_btndengyu,id_btnqingkong;

    int num1=0,num2=0,jieguo=0,flag=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        id_tvshow = findViewById(R.id.id_tvshow);
        id_btn7 = findViewById(R.id.id_btn7);
        id_btn8 = findViewById(R.id.id_btn8);
        id_btn9 = findViewById(R.id.id_btn9);
        id_btnjia = findViewById(R.id.id_btnjia);
        id_btnqingkong = findViewById(R.id.id_btnqingkong);
        id_btndengyu = findViewById(R.id.id_btndengyu);
        id_btncheng = findViewById(R.id.id_btncheng);
//        id_btn7.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                id_tvshow.setText("7");
//            }
//        });

        id_btn7.setOnClickListener(this); //alt+enter   enter  enter
        id_btn8.setOnClickListener(this);
        id_btn9.setOnClickListener(this);
        id_btnjia.setOnClickListener(this);
        id_btndengyu.setOnClickListener(this);
        id_btnqingkong.setOnClickListener(this);
        id_btncheng.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.id_btn7:
                id_tvshow.setText(id_tvshow.getText()+"7");
                jisuan(7); //alt+enter
                break;
            case R.id.id_btn8:
                id_tvshow.setText(id_tvshow.getText()+"8");
                jisuan(8); //alt+enter
                break;
            case R.id.id_btn9:
                id_tvshow.setText(id_tvshow.getText()+"9");
                jisuan(9); //alt+enter
                break;
            case R.id.id_btnjia:
                id_tvshow.setText(id_tvshow.getText()+"+");
                break;
            case R.id.id_btndengyu:
                if(id_tvshow.getText().toString().contains("+")){
                    jieguo =num1+num2;
                }else if(id_tvshow.getText().toString().contains("x")){
                    jieguo = num1*num2;
                }

                id_tvshow.setText(id_tvshow.getText()+"="+String.valueOf(jieguo));
                flag = 0;
                break;
            case R.id.id_btncheng:
                id_tvshow.setText(id_tvshow.getText()+"x");
                break;
            case R.id.id_btnqingkong:
                id_tvshow.setText("");
                break;
        }
    }

    private void jisuan(int num) {
        if(flag==0){
            num1 = num;
            flag=1;
        }else {
            num2 =num;
            flag=0;
        }
    }
}